package com.contract.spirit.func.dialog.base

import android.content.Context
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.contract.spirit.R
import com.contract.spirit.bean.entity.DialogBean
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.XPopupCallback
import com.lxj.xpopup.util.XPopupUtils

/**
 * @author: witness
 * created: 2021/9/24
 * desc:
 */
open class BaseCenterPopupView(context: Context,dialogBean: DialogBean,onDialogCallBack: BasePopDataBindImpl.OnDialogCallBack?) : CenterPopupView(context) {
    private var dialogClickCallBack: BasePopDataBindImpl.OnDialogCallBack? = null
    private var commonDialogInfo: DialogBean? = null
    init {
        XPopup.setShadowBgColor(context.resources.getColor(R.color.colorAlpha20))
        dialogClickCallBack = onDialogCallBack
        commonDialogInfo = dialogBean
    }

    override fun getMaxWidth(): Int {
        return XPopupUtils.getScreenWidth(context)
    }

    override fun onCreate() {
        super.onCreate()
        bindPopBasicData()
    }

    var popDataBindImpl: BasePopDataBindImpl? = null
    private fun bindPopBasicData() {
       val viewHolder = BaseViewHolder(popupContentView)
        if (popDataBindImpl == null) {
            popDataBindImpl = BasePopDataBindImpl()
        }
        dialogClickCallBack?.let { popDataBindImpl!!.setOnDialogListener(it) }
        commonDialogInfo?.let { popDataBindImpl!!.onBindData(this, viewHolder, it) }
    }

    companion object{
        fun showDialog(
            mContext: Context,
            baseCenterPopupView: BaseCenterPopupView?,
            xPopupCallback: XPopupCallback?=null
        ) {
            XPopup.setShadowBgColor(mContext.resources.getColor(R.color.colorAlpha20))
            val builder = XPopup.Builder(mContext)
            if (xPopupCallback!=null){
                builder.setPopupCallback(xPopupCallback)
            }
            builder.dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(baseCenterPopupView)
                .show()
        }
    }

}